home *** CD-ROM | disk | FTP | other *** search
/ Acorn User: China / Acorn User China CD-ROM (UK) (Disc A) / Acorn User China CD-ROM (UK) (Disc A).bin / DEMON / GNU / BISON124.ARC / c_files < prev    next >
Encoding:
Text File  |  1995-06-03  |  11.5 KB  |  496 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #if defined (VMS) & !defined (__VMS_POSIX)
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #ifndef XPFILE
  25. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  26. #endif
  27. #ifndef XPFILE1
  28. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  29. #endif
  30. #endif
  31.  
  32. #ifdef __riscos
  33. #define unlink remove
  34. #ifndef XPFILE
  35. #define XPFILE "File:bsn/simple"
  36. #endif
  37. #ifndef XPFILE1
  38. #define XPFILE1 "File:bsn/hairy"
  39. #endif
  40. #endif
  41.  
  42. #include <stdio.h>
  43. #include "system.h"
  44. #include "files.h"
  45. #include "new.h"
  46. #include "gram.h"
  47.  
  48. FILE *finput = NULL;
  49. FILE *foutput = NULL;
  50. FILE *fdefines = NULL;
  51. FILE *ftable = NULL;
  52. FILE *fattrs = NULL;
  53. FILE *fguard = NULL;
  54. FILE *faction = NULL;
  55. FILE *fparser = NULL;
  56.  
  57. /* File name specified with -o for the output file, or 0 if no -o.  */
  58. char *spec_outfile;
  59.  
  60. char *infile;
  61. char *outfile;
  62. char *defsfile;
  63. char *tabfile;
  64. char *attrsfile;
  65. char *guardfile;
  66. char *actfile;
  67. char *tmpattrsfile;
  68. char *tmptabfile;
  69. char *tmpdefsfile;
  70.  
  71. extern char    *mktemp();    /* So the compiler won't complain */
  72. extern char    *getenv();
  73. extern void    perror();
  74. FILE    *tryopen();    /* This might be a good idea */
  75. void done();
  76.  
  77. extern char *program_name;
  78. extern int verboseflag;
  79. extern int definesflag;
  80. int fixed_outfiles = 0;
  81.  
  82.  
  83. char*
  84. stringprepend(string1, string2, end2)
  85. char *string1;
  86. char *string2;
  87. int end2;
  88. {
  89.   register char *ostring;
  90.   register char *cp, *cp1;
  91.   register int i;
  92.  
  93.   cp = string1;  i = 0;
  94.   while (*cp++) i++;
  95.  
  96.   ostring = NEW2(i+end2+1, char);
  97.  
  98.   cp = ostring;
  99.   cp1 = string1;
  100.   while (*cp++ = *cp1++) ;
  101.   cp--;
  102.  
  103.   cp1 = string2;
  104.   for (i = 0; i < end2; i++)
  105.     *cp++ = *cp1++;
  106.  
  107.   return ostring;
  108. }
  109.  
  110. char*
  111. stringappend(string1, end1, string2)
  112. char *string1;
  113. int end1;
  114. char *string2;
  115. {
  116.   register char *ostring;
  117.   register char *cp, *cp1;
  118.   register int i;
  119.  
  120.   cp = string2;  i = 0;
  121.   while (*cp++) i++;
  122.  
  123.   ostring = NEW2(i+end1+1, char);
  124.  
  125.   cp = ostring;
  126.   cp1 = string1;
  127.   for (i = 0; i < end1; i++)
  128.     *cp++ = *cp1++;
  129.  
  130.   cp1 = string2;
  131.   while (*cp++ = *cp1++) ;
  132.  
  133.   return ostring;
  134. }
  135.  
  136.  
  137. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  138.    the output files, and opens the tmp files and the parser */
  139. void
  140. openfiles()
  141. {
  142.   char *name_base;
  143.   register char *cp;
  144.   char *filename;
  145.   int base_length;
  146.   int short_base_length;
  147. #ifdef __riscos
  148.   char *tmpfnam;
  149. #endif
  150.  
  151. #ifdef __riscos
  152.   char *tmp_base = "<Wimp$ScrapDir>.b_";
  153. #else
  154. #if defined (VMS) & !defined (__VMS_POSIX)
  155.   char *tmp_base = "sys$scratch:b_";
  156. #else
  157.   char *tmp_base = "/tmp/b.";
  158. #endif
  159. #endif
  160.   int tmp_len;
  161.  
  162. #ifdef MSDOS
  163.   tmp_base = getenv ("TMP");
  164.   if (tmp_base == 0)
  165.     tmp_base = "";
  166.   strlwr (infile);
  167. #endif /* MSDOS */
  168.  
  169.   tmp_len = strlen (tmp_base);
  170.  
  171.   if (spec_outfile)
  172.     {
  173.       /* -o was specified.  The precise -o name will be used for ftable.
  174.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  175.       name_base = spec_outfile;
  176. #ifdef MSDOS
  177.       strlwr (name_base);
  178. #endif /* MSDOS */
  179.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  180.       base_length = strlen (name_base);
  181.       if (!strcmp (name_base + base_length - 2, ".c"))
  182.     base_length -= 2;
  183. #ifdef __riscos
  184.       else if (strncmp(name_base, "c.", 2) == 0) {
  185.         name_base += 2;
  186.         base_length -= 2;
  187.       }
  188. #endif
  189.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  190.       short_base_length = base_length;
  191.       if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  192.     short_base_length -= 4;
  193.       else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  194.     short_base_length -= 4;
  195.     }
  196.   else if (spec_file_prefix)
  197.     {
  198.       /* -b was specified.  Construct names from it.  */
  199.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  200.       short_base_length = strlen (spec_file_prefix);
  201.       /* Count room for `.tab'.  */
  202.       base_length = short_base_length + 4;
  203.       name_base = (char *) xmalloc (base_length + 1);
  204.       /* Append `.tab'.  */
  205.       strcpy (name_base, spec_file_prefix);
  206. #ifdef __riscos
  207.       strcat (name_base, "_tab");
  208. #else
  209. #ifdef VMS
  210.       strcat (name_base, "_tab");
  211. #else
  212.       strcat (name_base, ".tab");
  213. #endif
  214. #endif
  215. #ifdef MSDOS
  216.       strlwr (name_base);
  217. #endif /* MSDOS */
  218.     }
  219.   else
  220.     {
  221.       /* -o was not specified; compute output file name from input
  222.      or use y.tab.c, etc., if -y was specified.  */
  223.  
  224.       name_base = fixed_outfiles ? "y.y" : infile;
  225.  
  226.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  227.  
  228.       base_length = strlen (name_base);
  229.       if (!strcmp (name_base + base_length - 2, ".y"))
  230.     base_length -= 2;
  231. #ifdef __riscos
  232.       else if (strncmp(name_base, "y.", 2) == 0) {
  233.         name_base += 2;
  234.         base_length -= 2;
  235.       }
  236. #endif
  237.       short_base_length = base_length;
  238.  
  239. #ifdef __riscos
  240.       name_base = stringappend(name_base, short_base_length, "_tab");
  241. #else
  242. #ifdef VMS
  243.       name_base = stringappend(name_base, short_base_length, "_tab");
  244. #else
  245. #ifdef MSDOS
  246.       name_base = stringappend(name_base, short_base_length, "_tab");
  247. #else
  248.       name_base = stringappend(name_base, short_base_length, ".tab");
  249. #endif /* not MSDOS */
  250. #endif
  251. #endif
  252.       base_length = short_base_length + 4;
  253.     }
  254.  
  255.   finput = tryopen(infile, "r");
  256.  
  257.   filename = getenv("BISON_SIMPLE");
  258. #ifdef MSDOS
  259.   /* File doesn't exist in current directory; try in INIT directory.  */
  260.   cp = getenv("INIT");
  261.   if (filename == 0 && cp != NULL)
  262.     {
  263.       filename = xmalloc(strlen(cp) + strlen(PFILE) + 2);
  264.       strcpy(filename, cp);
  265.       cp = filename + strlen(filename);
  266.       *cp++ = '/';
  267.       strcpy(cp, PFILE);
  268.     }
  269. #endif /* MSDOS */
  270.   fparser = tryopen(filename ? filename : PFILE, "r");
  271.  
  272.   if (verboseflag)
  273.     {
  274. #ifdef __riscos
  275.       outfile = stringprepend("out.", name_base, short_base_length);
  276. #else
  277. #ifdef MSDOS
  278.       outfile = stringappend(name_base, short_base_length, ".out");
  279. #else
  280.       /* We used to use just .out if spec_name_prefix (-p) was used,
  281.      but that conflicts with Posix.  */
  282.       outfile = stringappend(name_base, short_base_length, ".output");
  283. #endif
  284. #endif
  285.       foutput = tryopen(outfile, "w");
  286.     }
  287.  
  288. #ifdef __riscos
  289.   actfile = mktemp("BsnAct");
  290.   tmpattrsfile = mktemp("BsnAttr");
  291.   tmptabfile = mktemp("BsnTab");
  292.   tmpdefsfile = mktemp("BsnDefs");
  293. #else
  294. #ifdef MSDOS
  295.   actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  296.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  297.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  298.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "deXXXXXX"));
  299. #else
  300.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  301.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  302.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  303.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "defs.XXXXXX"));
  304. #endif /* not MSDOS */
  305. #endif /* not RISC OS */
  306.  
  307.   faction = tryopen(actfile, "w+");
  308.   fattrs = tryopen(tmpattrsfile,"w+");
  309.   ftable = tryopen(tmptabfile, "w+");
  310.  
  311.   if (definesflag)
  312.     {
  313. #ifdef __riscos
  314.       defsfile = stringprepend("h.", name_base, base_length);
  315. #else
  316.       defsfile = stringappend(name_base, base_length, ".h");
  317. #endif
  318.       fdefines = tryopen(tmpdefsfile, "w+");
  319.     }
  320.  
  321. #ifndef MSDOS
  322.   unlink(actfile);
  323.   unlink(tmpattrsfile);
  324.   unlink(tmptabfile);
  325.   unlink(tmpdefsfile);
  326. #endif
  327.  
  328.     /* These are opened by `done' or `open_extra_files', if at all */
  329.   if (spec_outfile)
  330.     tabfile = spec_outfile;
  331.   else
  332. #ifdef __riscos
  333.     tabfile = stringprepend("c.", name_base, base_length);
  334. #else
  335.     tabfile = stringappend(name_base, base_length, ".c");
  336. #endif
  337.  
  338. #ifdef __riscos
  339.   tmpfnam = stringappend(name_base, short_base_length, "_typ");
  340.   attrsfile = stringappend("h.", 2, tmpfnam);
  341.   FREE(tmpfnam);
  342.   tmpfnam = stringappend(name_base, short_base_length, "_grd");
  343.   guardfile = stringappend("c.", 2, tmpfnam);
  344.   FREE(tmpfnam);
  345. #else
  346. #ifdef VMS
  347.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  348.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  349. #else
  350. #ifdef MSDOS
  351.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  352.   guardfile = stringappend(name_base, short_base_length, ".guc");
  353. #else
  354.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  355.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  356. #endif /* not MSDOS */
  357. #endif /* not VMS */
  358. #endif /* not RISC OS */
  359. }
  360.  
  361.  
  362.  
  363. /* open the output files needed only for the semantic parser.
  364. This is done when %semantic_parser is seen in the declarations section.  */
  365.  
  366. void
  367. open_extra_files()
  368. {
  369.   FILE *ftmp;
  370.   int c;
  371.   char *filename, *cp;
  372.  
  373.   fclose(fparser);
  374.  
  375.   filename = (char *) getenv ("BISON_HAIRY");
  376. #ifdef MSDOS
  377.   /* File doesn't exist in current directory; try in INIT directory.  */
  378.   cp = getenv("INIT");
  379.   if (filename == 0 && cp != NULL)
  380.     {
  381.       filename = xmalloc(strlen(cp) + strlen(PFILE1) + 2);
  382.       strcpy(filename, cp);
  383.       cp = filename + strlen(filename);
  384.       *cp++ = '/';
  385.       strcpy(cp, PFILE1);
  386.     }
  387. #endif
  388.   fparser= tryopen(filename ? filename : PFILE1, "r");
  389.  
  390.         /* JF change from inline attrs file to separate one */
  391.   ftmp = tryopen(attrsfile, "w");
  392.   rewind(fattrs);
  393.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  394.     putc(c,ftmp);
  395.   fclose(fattrs);
  396.   fattrs=ftmp;
  397.  
  398.   fguard = tryopen(guardfile, "w");
  399.  
  400. }
  401.  
  402.     /* JF to make file opening easier.  This func tries to open file
  403.        NAME with mode MODE, and prints an error message if it fails. */
  404. FILE *
  405. tryopen(name, mode)
  406. char *name;
  407. char *mode;
  408. {
  409.   FILE    *ptr;
  410.  
  411.   ptr = fopen(name, mode);
  412.   if (ptr == NULL)
  413.     {
  414.       fprintf(stderr, "%s: ", program_name);
  415.       perror(name);
  416.       done(2);
  417.     }
  418.   return ptr;
  419. }
  420.  
  421. void
  422. done(k)
  423. int k;
  424. {
  425.   if (faction)
  426.     fclose(faction);
  427.  
  428.   if (fattrs)
  429.     fclose(fattrs);
  430.  
  431.   if (fguard)
  432.     fclose(fguard);
  433.  
  434.   if (finput)
  435.     fclose(finput);
  436.  
  437.   if (fparser)
  438.     fclose(fparser);
  439.  
  440.   if (foutput)
  441.     fclose(foutput);
  442.  
  443.     /* JF write out the output file */
  444.   if (k == 0 && ftable)
  445.     {
  446.       FILE *ftmp;
  447.       register int c;
  448.  
  449.       ftmp=tryopen(tabfile, "w");
  450.       rewind(ftable);
  451.       while((c=getc(ftable)) != EOF)
  452.         putc(c,ftmp);
  453.       fclose(ftmp);
  454.       fclose(ftable);
  455.  
  456.       if (definesflag)
  457.         {
  458.           ftmp = tryopen(defsfile, "w");
  459.           fflush(fdefines);
  460.           rewind(fdefines);
  461.           while((c=getc(fdefines)) != EOF)
  462.             putc(c,ftmp);
  463.           fclose(ftmp);
  464.           fclose(fdefines);
  465.         }
  466.     }
  467.  
  468. #if defined (VMS) & !defined (__VMS_POSIX)
  469.   if (faction)
  470.     delete(actfile);
  471.   if (fattrs)
  472.     delete(tmpattrsfile);
  473.   if (ftable)
  474.     delete(tmptabfile);
  475.   if (k==0) sys$exit(SS$_NORMAL);
  476.   sys$exit(SS$_ABORT);
  477. #else
  478. #ifdef __riscos
  479.   if (faction)
  480.     remove(actfile);
  481.   if (fattrs)
  482.     remove(tmpattrsfile);
  483.   if (ftable)
  484.     remove(tmptabfile);
  485. #else
  486. #ifdef MSDOS
  487.   if (actfile) unlink(actfile);
  488.   if (tmpattrsfile) unlink(tmpattrsfile);
  489.   if (tmptabfile) unlink(tmptabfile);
  490.   if (tmpdefsfile) unlink(tmpdefsfile);
  491. #endif /* MSDOS */
  492. #endif /* RISC OS */
  493.   exit(k);
  494. #endif /* not VMS, or __VMS_POSIX */
  495. }
  496.